<?php
namespace app\modules\Cruise\controllers;

use Yii;
use app\modules\Cruise\models\Image;

class ImageController extends \yii\web\Controller
{

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionAddName(){//{{{
        $id = Yii::$app->request->get('id'); 
        $name = Yii::$app->request->get('name'); 

        $ret = false;

        if(!empty($id) && !empty($name) ){
            $model = Image::findOne($id);            
            $model->name = $name;
            
            $ret = $model->save();
        }        
        
        echo $ret; 
    }//}}}

    // 指定图片为封面
    public function actionCover(){//{{{
        $id = Yii::$app->request->get('id'); 

        $ret = false;

        if(!empty($id)){
            $model = Image::findOne($id);            
            $all = Image::find()->where(['product_id'=>$model->product_id])->all();    
            foreach($all as $val){
                $val->cover = 0; 
                $val->save();
            }
            $model->cover = 1;
            $ret = $model->save();
        }        
        
        echo $ret; 
    }//}}}


    // 删除图片
    public function actionDel(){//{{{
        $id = Yii::$app->request->get('id'); 
        
        $ret = false;

        if(!empty($id)){
            $model = Image::findOne($id);            
            $model->status  = 2;
            $ret = $model->save();
        }       

        echo $ret;
    }//}}}





}
